home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / strlib.zip / STRTOK.C < prev    next >
Text File  |  1993-01-04  |  2KB  |  60 lines

  1.  
  2. /*  File   : strtok.c
  3.     Author : Richard A. O'Keefe.
  4.     Updated: 11 April 1984
  5.     Defines: istrtok(), strtok()
  6.  
  7.     strtok(src, set)
  8.         skips over initial characters of src[] which occur in set[].
  9.         The result is a pointer to the first character of src[]
  10.         which does not occur in set[].  It then skips until it finds
  11.         a character which does occur in set[], and changes it to NUL.
  12.         If src is NullS, it is as if you had specified the place
  13.         just after the last NUL was written.  If src[] contains no
  14.         characters which are not in set[] (e.g. if src == "") the
  15.         result is NullS.
  16.  
  17.         To read a sequence of words separated by spaces you might write
  18.         p = strtok(sequence, " ");
  19.         while (p) {process_word(p); p = strtok(NullS, " ");}
  20.         This is unpleasant, so there is also a function
  21.  
  22.     istrtok(src, set)
  23.         which builds the set and notes the source string for future
  24.         reference.  With this function, you can write
  25.  
  26.         for (istrtok(wordlist, " \t"); p = strtok(NullS, NullS); )
  27.             process_word(p);
  28. */
  29.  
  30. #include "strings.h"
  31. #include "_str2set.h"
  32.  
  33. static  char    *oldSrc = "";
  34.  
  35. void istrtok(src, set)
  36.     char *src, *set;
  37.     {
  38.         _str2set(set);
  39.         if (src != NullS) oldSrc = src;
  40.     }
  41.  
  42.  
  43. char *strtok(src, set)
  44.     register char *src;
  45.     char *set;
  46.     {
  47.         char *save;
  48.  
  49.         _str2set(set);
  50.         if (src == NullS) src = oldSrc;
  51.         while (_set_vec[*src] == _set_ctr) src++;
  52.         if (!*src) return NullS;
  53.         save = src;
  54.         while (_set_vec[*++src] != _set_ctr) ;
  55.         *src++ = NUL;
  56.         oldSrc = src;
  57.         return save;
  58.     }
  59.  
  60.